package com.threader.ui;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.table.AbstractTableModel;

import com.threader.bo.SomeObject;

public class SomeObjectsTableModel extends AbstractTableModel implements Observer {
	private static final long serialVersionUID = 1L;
	private final String[] columnNames = { "Name"};
	
	private final Class[] columnClasses = { String.class, String.class, String.class, String.class, String.class};
	
	ArrayList<SomeObject> ObjectsList = new ArrayList<SomeObject>();

	public SomeObjectsTableModel(){
		super();
	}

	@Override
	public void update(Observable o, Object arg) {
		int index = ObjectsList.indexOf(o);
		fireTableRowsUpdated(index, index);
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Class<?> getColumnClass(int col) {
		return columnClasses[col];
	}

	@Override
	public int getRowCount() {
		return ObjectsList.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		SomeObject t = ObjectsList.get(rowIndex);
	    switch (columnIndex) {
	    case 0: // Name
	    	return t.Name;
	    default: return "";
	    }

	}
	public void addObject(SomeObject so) {
		so.addObserver(this);
		ObjectsList.add(so);
		fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
	}

	public void delObject(int row) {
		SomeObject so = ObjectsList.get(row);
		so.deleteObserver(this);
		ObjectsList.remove(row);
		fireTableRowsDeleted(row, row);
	}
	
	public SomeObject getObject(int selectedRow) {
		return ObjectsList.get(selectedRow);
	}

}
